//
//  DescribeView.m
//  DaDaBao
//
//  Created by user on 15/8/14.
//  Copyright (c) 2015年 User. All rights reserved.
//
#import "DescribeView.h"
#import "ProjectDesModel.h"
@interface DescribeView()<UIWebViewDelegate>
@property (nonatomic,strong) UIScrollView * backScrollView;//背景滑动视图
@property (nonatomic,strong) UIView * foundationDescribeView;//基础描述view
@property (nonatomic,strong) UIView * informationView;//保理信息view
@property (nonatomic,strong) UIView * riskGuraranteeView;//风险保障view
@property (nonatomic, strong) NSString *productId;
@property (nonatomic, strong) NSString *productName;
@property(nonatomic,strong) UIButton *noDataBt;
@property(nonatomic,strong)UIButton *tipBt;
@end
@implementation DescribeView
-(id)initWithFrame:(CGRect)frame andProductID:(NSString *)productId andProductName:(NSString *)productName
{
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = [UIColor grayColor];
        
        _backScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        _backScrollView.backgroundColor = [UIColor hexStringToColor:@"#ebebeb"];
        [self addSubview:_backScrollView];
        
        _productId = productId;
        _productName = productName;
        [self addSubview:self.noDataBt];
        [self addSubview:self.tipBt];
        [self getData];
        
    }
    return self;
}
-(void)getData
{
    NSDictionary *dict = @{ @"productId" : _productId};

    [MBProgressHUD showHUDAddedTo:self animated:TRUE];
    _noDataBt.hidden = TRUE;
    _tipBt.hidden = TRUE;
    
    [AppGlobalClass PostDataWithNSstring:MORE_DETAIL_INFO_URL withDictionary:dict success:^(NSDictionary *dictionary){
        
        [MBProgressHUD hideAllHUDsForView:self animated:TRUE];
        if([[dictionary objectForKey:@"success"] isEqualToNumber:[NSNumber numberWithBool:TRUE]])
        {
            ProjectDesModel *projectDesModel = [[ProjectDesModel alloc]initWithDictionary:[[[[dictionary objectForKey:@"responseData"]firstObject] objectForKey:@"rows"] firstObject]];
            
            [self createTheFoundationDescribeViewWithTitle:[NSString stringWithFormat:@"%@   基础描述",_productName] andDetailString:projectDesModel.productDesc];//创建基础描述view
            
            [self createTheInformationViewWithTitle:[NSString stringWithFormat:@"%@   保理信息",_productName] andModel:(projectDesModel)];//创建保理信息View
            [self createTheriskGuraranteeViewWithTitle:[NSString stringWithFormat:@"%@   风险保障",_productName] andModel:(projectDesModel)];//创建风险担保view
            
            
            _backScrollView.contentSize = CGSizeMake(self.frame.size.width, _foundationDescribeView.frame.size.height + _informationView.frame.size.height + _riskGuraranteeView.frame.size.height + 20);
        }
        else
        {
            ALERT_VIEW([dictionary objectForKey:@"message"]);
        }
    } failure:^(NSError *error){
        [MBProgressHUD hideAllHUDsForView:self animated:TRUE];
        _noDataBt.hidden = FALSE;
        _tipBt.hidden = TRUE;
    } networkerror:^(NSString *networkerror){
        [MBProgressHUD hideAllHUDsForView:self animated:TRUE];
        _noDataBt.hidden = TRUE;
        _tipBt.hidden = FALSE;
        
    }];

}
//没网提示
-(UIButton *)tipBt
{
    if (!_tipBt) {
        _tipBt = [[UIButton alloc]initWithFrame:CGRectMake((SCREEN_WIDTH - 116.0f) / 2.0f, (SCREEN_HEIGHT - 130.0f) / 2.0f-64.0f, 116.0f, 130.0f)];
        [_tipBt setBackgroundImage:[UIImage imageNamed:@"network_error"] forState:UIControlStateNormal];
        [_tipBt addTarget:self action:@selector(buttonAgainTapped:) forControlEvents:UIControlEventTouchUpInside];
        _tipBt.hidden = TRUE;
    }
    return _tipBt;
}

- (UIButton *)noDataBt
{
    if (!_noDataBt) {
        _noDataBt = [[UIButton alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 116.0f) / 2.0f, (SCREEN_HEIGHT - 130.0f) / 2.0f-64.0f, 116.0f, 130.0f)];
        [_noDataBt addTarget:self action:@selector(buttonAgainTapped:) forControlEvents:UIControlEventTouchUpInside];
        [_noDataBt setImage:[UIImage imageNamed:@"img_nodata"] forState:UIControlStateNormal];
        _noDataBt.hidden = TRUE;
    }
    return _noDataBt;
}
- (void)buttonAgainTapped:(id)sender
{
    [self getData];
}

#pragma mark----------创建基础描述信息
-(void)createTheFoundationDescribeViewWithTitle:(NSString *)title andDetailString:(NSString *)detailString
{
    CGFloat titleHeight = [UILabel height:title widthOfFatherView:self.frame.size.width-20 textFont:[UIFont systemFontOfSize:17]];
    
    _foundationDescribeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,self.frame.size.width,200+titleHeight + 40.5)];
    _foundationDescribeView.backgroundColor = [UIColor whiteColor];
    [_backScrollView addSubview:_foundationDescribeView];
    
    UILabel * titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, self.frame.size.width-20, titleHeight)];
    titleLabel.text = title;
    titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    titleLabel.numberOfLines = 0;
    [_foundationDescribeView addSubview:titleLabel];
    
    UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(0, titleHeight + 20, self.frame.size.width, 0.5)];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [_foundationDescribeView addSubview:lineView];
    
    UIWebView * webView = [[UIWebView alloc]initWithFrame:CGRectMake(10, titleHeight + 30.5, self.frame.size.width - 20, 200)];
    webView.backgroundColor = [UIColor hexStringToColor:@"#ebebeb"];
    [webView loadHTMLString:detailString baseURL:nil];
    webView.delegate = self;
    [_foundationDescribeView addSubview:webView];
}
#pragma mark----------创建保理信息
-(void)createTheInformationViewWithTitle:(NSString *)title andModel:(ProjectDesModel *)model
{
  
    CGFloat height1 = [UILabel height:model.factoringOrg widthOfFatherView:self.frame.size.width-20 textFont:[UIFont systemFontOfSize:14]];
    CGFloat height2 = [UILabel height:model.factoringBusiness widthOfFatherView:self.frame.size.width-20 textFont:[UIFont systemFontOfSize:14]];
    CGFloat height3 = [UILabel height:model.factoringDesc widthOfFatherView:self.frame.size.width-20 textFont:[UIFont systemFontOfSize:14]];
    CGFloat height4 = [UILabel height:model.factoringSuggest widthOfFatherView:self.frame.size.width-20 textFont:[UIFont systemFontOfSize:14]];
    
    NSArray * nameArr = @[@"保理机构",@"保理业务描述",@"保理公司简介",@"保理公司建议"];
    
    CGFloat titleHeight = [UILabel height:title widthOfFatherView:self.frame.size.width-20 textFont:[UIFont systemFontOfSize:17]];

    _informationView = [[UIView alloc] initWithFrame:CGRectMake(0, _foundationDescribeView.frame.size.height+5,self.frame.size.width,height1 +height2 + height3 +height4 + titleHeight + 40.5 + 80)];
    _informationView.backgroundColor = [UIColor whiteColor];
    [_backScrollView addSubview:_informationView];
  //大标题
    UILabel * titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, self.frame.size.width-20, titleHeight)];
    titleLabel.text = title;
    titleLabel.numberOfLines = 0;
    titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    [_informationView addSubview:titleLabel];
  //隔线
    UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(0, titleHeight + 20, self.frame.size.width, 0.5)];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [_informationView addSubview:lineView];
   //保理机构
    UILabel * Orglabel = [[UILabel alloc]initWithFrame:CGRectMake(10, titleHeight + 30.5, self.frame.size.width - 20, 20)];
    Orglabel.text = nameArr[0];
    Orglabel.font = [UIFont systemFontOfSize:15];
    [_informationView addSubview:Orglabel];
        
    UILabel * detailLabelOrg = [[UILabel alloc]initWithFrame:CGRectMake(10, titleHeight + 50.5, self.frame.size.width - 20, height1)];
    detailLabelOrg.text = model.factoringOrg;
    detailLabelOrg.numberOfLines = 0;
    detailLabelOrg.font = [UIFont systemFontOfSize:14];
    detailLabelOrg.textColor = [UIColor grayColor];
    detailLabelOrg.lineBreakMode = NSLineBreakByWordWrapping;
    [_informationView addSubview:detailLabelOrg];
   //保理业务
    UILabel * buslabel = [[UILabel alloc]initWithFrame:CGRectMake(10, titleHeight + 50.5 + height1, self.frame.size.width - 20, 20)];
    buslabel.text = nameArr[1];
    buslabel.font = [UIFont systemFontOfSize:15];
    [_informationView addSubview:buslabel];
    
    UILabel * detailLabelBus = [[UILabel alloc]initWithFrame:CGRectMake(10, titleHeight + 70.5 +height1, self.frame.size.width - 20, height2)];
    detailLabelBus.text = model.factoringBusiness;
    detailLabelBus.numberOfLines = 0;
    detailLabelBus.font = [UIFont systemFontOfSize:14];
    detailLabelBus.textColor = [UIColor grayColor];
    detailLabelBus.lineBreakMode = NSLineBreakByWordWrapping;
    [_informationView addSubview:detailLabelBus];
    //保理公司简介
    UILabel * desclabel = [[UILabel alloc]initWithFrame:CGRectMake(10, titleHeight + 70.5 + height1 + height2, self.frame.size.width - 20, 20)];
    desclabel.text = nameArr[2];
    desclabel.font = [UIFont systemFontOfSize:15];
    [_informationView addSubview:desclabel];
    
    UILabel * detailLabelDesc = [[UILabel alloc]initWithFrame:CGRectMake(10, titleHeight + 90.5 +height1 + height2, self.frame.size.width - 20, height3)];
    detailLabelDesc.text = model.factoringDesc;
    detailLabelDesc.numberOfLines = 0;
    detailLabelDesc.font = [UIFont systemFontOfSize:14];
    detailLabelDesc.textColor = [UIColor grayColor];
    detailLabelDesc.lineBreakMode = NSLineBreakByWordWrapping;
    [_informationView addSubview:detailLabelDesc];
    
    //保理公司简介
    UILabel * sugdesclabel = [[UILabel alloc]initWithFrame:CGRectMake(10, titleHeight + 90.5 + height1 + height2 + height3, self.frame.size.width - 20, 20)];
    sugdesclabel.text = nameArr[3];
    sugdesclabel.font = [UIFont systemFontOfSize:15];
    [_informationView addSubview:sugdesclabel];
    
    UILabel * detailLabelSug = [[UILabel alloc]initWithFrame:CGRectMake(10, titleHeight + 110.5 + height1 + height2 + height3, self.frame.size.width - 20, height4)];
    detailLabelSug.text = model.factoringSuggest;
    detailLabelSug.numberOfLines = 0;
    detailLabelSug.font = [UIFont systemFontOfSize:14];
    detailLabelSug.textColor = [UIColor grayColor];
    detailLabelSug.lineBreakMode = NSLineBreakByWordWrapping;
    [_informationView addSubview:detailLabelSug];

    
}
#pragma mark----------创建风险保障view
-(void)createTheriskGuraranteeViewWithTitle:(NSString *)title andModel:(ProjectDesModel *)model
{
    CGFloat height1 = [UILabel height:model.refundSource widthOfFatherView:self.frame.size.width-20 textFont:[UIFont systemFontOfSize:14]];
    CGFloat height2 = [UILabel height:model.controlStep widthOfFatherView:self.frame.size.width-20 textFont:[UIFont systemFontOfSize:14]];
    
    CGFloat titleHeight = [UILabel height:title widthOfFatherView:self.frame.size.width-20 textFont:[UIFont systemFontOfSize:17]];
    
    _riskGuraranteeView = [[UIView alloc] initWithFrame:CGRectMake(0, _foundationDescribeView.frame.size.height+10 +_informationView.frame.size.height,self.frame.size.width,height1 + height2 + 40 +titleHeight + 40.5)];
    _riskGuraranteeView.backgroundColor = [UIColor whiteColor];
    [_backScrollView addSubview:_riskGuraranteeView];
    
    UILabel * titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, self.frame.size.width-20,titleHeight)];
    titleLabel.text = title;
    titleLabel.numberOfLines = 0;
    titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    [_riskGuraranteeView addSubview:titleLabel];
    
    UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(0, titleHeight + 20, self.frame.size.width, 0.5)];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [_riskGuraranteeView addSubview:lineView];
    
    //保理机构
    UILabel * Orglabel = [[UILabel alloc]initWithFrame:CGRectMake(10, titleHeight + 30.5, self.frame.size.width - 20, 20)];
    Orglabel.text = @"还款来源";
    Orglabel.font = [UIFont systemFontOfSize:15];
    [_riskGuraranteeView addSubview:Orglabel];
    
    UILabel * detailLabelOrg = [[UILabel alloc]initWithFrame:CGRectMake(10, titleHeight + 50.5, self.frame.size.width - 20, height1)];
    detailLabelOrg.text = model.refundSource;
    detailLabelOrg.numberOfLines = 0;
    detailLabelOrg.font = [UIFont systemFontOfSize:14];
    detailLabelOrg.textColor = [UIColor grayColor];
    detailLabelOrg.lineBreakMode = NSLineBreakByWordWrapping;
    [_riskGuraranteeView addSubview:detailLabelOrg];
    //保理业务
    UILabel * buslabel = [[UILabel alloc]initWithFrame:CGRectMake(10, titleHeight + 50.5 + height1, self.frame.size.width - 20, 20)];
    buslabel.text = @"风控措施";
    buslabel.font = [UIFont systemFontOfSize:15];
    [_riskGuraranteeView addSubview:buslabel];
    
    UILabel * detailLabelBus = [[UILabel alloc]initWithFrame:CGRectMake(10, titleHeight + 70.5 +height1, self.frame.size.width - 20, height2)];
    detailLabelBus.text = model.controlStep;
    detailLabelBus.numberOfLines = 0;
    detailLabelBus.font = [UIFont systemFontOfSize:14];
    detailLabelBus.textColor = [UIColor grayColor];
    detailLabelBus.lineBreakMode = NSLineBreakByWordWrapping;
    [_riskGuraranteeView addSubview:detailLabelBus];

}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    //清除内存
    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
    [[NSUserDefaults standardUserDefaults]synchronize];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
